package main.java.thread;

/**
 * 演示多线程并发安全问题
 * 当多个线程并发操作同一临界资源。由于线程切换时机不确定，导致操作顺序混乱从而引发不良后果
 * 临界资源：操作资源的完成过程同一时刻只能允许单线程进行
 */
public class SyncDemo1 {
    public static boolean success1 = false;
    public static boolean success2 = false;
    public static void main(String[] args) {
        Bank b = new Bank();
        //while (true) {
            Thread t1 = new Thread() {
            public void run() {
                success1 = b.getMoney(20000);
            }
        };
            Thread t2 = new Thread() {
            public void run() {
                success2 = b.getMoney(20000);
            }
        };

            t1.start();
            t2.start();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (success1 && success2) {
                System.out.println("bug成功");
                //break;
            } else {
                System.out.println("仅有一人成功");
              //
        }
    }
}

